package com.sshtools.view.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public final class ServerTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;

	private List<TableItem> items; // 表格列表
	private final List<TableHead> heads; // 表头

	public ServerTableModel() {
		items = new ArrayList<>();
		heads = new ArrayList<>(Arrays.asList(new TableHead("", Boolean.class, true) //
				, new TableHead("server", String.class, false)//
				, new TableHead("desc", String.class, false)//
		));
	}

	public void addAll(List<ServerModel> servers) {
		for (ServerModel server : servers) {
			add(server);
		}
	}

	public void add(ServerModel server) {
		TableItem item = new TableItem();
		item.select = false;
		item.name = server.getName();
		item.desc = server.getDomain();
		item.data = server;
		items.add(item);
	}

	/** 提取所有选择的服务器列表 **/
	public List<ServerModel> getSelectedServers() {
		List<ServerModel> list = new ArrayList<ServerModel>();
		for (int i = 0; i < items.size(); i++) {
			TableItem item = items.get(i);
			if (item == null || !item.select) {
				continue;
			}
			list.add(item.data);
		}
		return list;
	}

	/** 获取选择的数量 **/
	public int getSelectedNum() {
		List<ServerModel> list = getSelectedServers();
		return (list != null) ? list.size() : 0;
	}

	/** 全部选择 **/
	public void selectAll() {
		for (int i = 0; i < items.size(); i++) {
			TableItem item = items.get(i);
			if (item == null) {
				continue;
			}
			item.select = true;
		}
		fireTableDataChanged();
	}

	/** 全部取消选择 **/
	public void unselectAll() {
		for (int i = 0; i < items.size(); i++) {
			TableItem item = items.get(i);
			if (item == null) {
				continue;
			}
			item.select = false;
		}
		fireTableDataChanged();
	}

	/** 全部反向选择 **/
	public void invertSelectedAll() {
		for (int i = 0; i < items.size(); i++) {
			TableItem item = items.get(i);
			if (item == null) {
				continue;
			}
			item.select = !item.select;
		}
		fireTableDataChanged();
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return heads.get(columnIndex).canEdit;
	}

	@Override
	public int getColumnCount() {
		return heads.size();
	}

	@Override
	public String getColumnName(int column) {
		return heads.get(column).title;
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return heads.get(columnIndex).classType;
	}

	@Override
	public int getRowCount() {
		return items.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		TableItem item = items.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return item.select;
		case 1:
			return item.name;
		case 2:
			return item.desc;
		}
		return null;
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		TableItem item = items.get(rowIndex);
		switch (columnIndex) {
		case 0:
			item.select = (Boolean) aValue;
			break;
		case 1:
			item.name = (String) aValue;
			break;
		case 2:
			item.desc = (String) aValue;
			break;
		default:
			return;
		}
		fireTableDataChanged();
	}

	/** 单元元素 **/
	private static class TableItem {
		public ServerModel data; // 数据
		public boolean select; // 是否选中
		public String name; // 显示名称
		public String desc; // 说明

	}

	/** 表头 **/
	private static class TableHead {
		public String title; // 名称
		public boolean canEdit; // 是否能编辑
		public Class<?> classType; // 类型

		public TableHead(String title, Class<?> classType, boolean canEdit) {
			this.title = title;
			this.canEdit = canEdit;
			this.classType = classType;
		}

	}

}
